package com.ztjava.pay.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseService;
import com.ztjava.core.bean.sub.PayBean;
import com.ztjava.core.client.Client;
import com.ztjava.core.utiliy.StringUtil;
import com.ztjava.pay.model.PayOrder;
import com.ztjava.plugin.event.EventKit;
import com.ztjava.plugin.event.core.RequestEvent;

public class PayService extends BaseService{
	/***
	 * 新建支付订单
	 * @param payBean
	 * @return
	 */
	public Ret insertOrder(PayBean payBean){
		PayOrder payOrder = new PayOrder();
		payOrder.set("model", payBean.getModel());
		payOrder.set("title", payBean.getTitle());
		payOrder.set("order_info", payBean.getOrder_info());
		payOrder.set("money", payBean.getMoney());
		payOrder.set("status", "READY");
		payOrder.set("expire",DateUtil.offsetHour(new Date(), 1).toTimestamp());
		payOrder.set("redirect_url", payBean.getRedirect_url());
		payOrder.set("notify_event", payBean.getNotify_event());
		payOrder.set("create_user", payBean.getCreate_user());
		payOrder.save();
		payOrder.set("num", StringUtil.orderNum("PA", payOrder.getInt("id"))).update();
		return Ret.create(Constant.RESULT, Constant.SUCCESS).set(Constant.MESSAGE, "建单成功！").set("num", payOrder.getStr("num"));
	}
	/***
	 * 根据订单号获取订单
	 * @param num
	 * @return
	 */
	public PayOrder getOrderByNum(String num){
		return PayOrder.dao.findFirst("select * from "+PayOrder.tablename+" where num=?",num);
	}
	public void payChoose(PayOrder payOrder,String pay_channel){
		payOrder.set("pay_channel", pay_channel).set("pay_time", DateUtil.date().toTimestamp()).update();
	}
	/***
	 * 支付成功
	 * @param payOrder
	 * @param pay_num
	 */
	public void paySuccess(PayOrder payOrder,String pay_num){
		payOrder.set("status","PAID").set("pay_result", Constant.SUCCESS).set("pay_msg", "支付成功！").set("pay_num", pay_num);
		List<Ret> retList = EventKit.postRemote(payOrder.getStr("notify_event"), new RequestEvent(payOrder.toRecord()));
		if(retList.size()==0){
			payOrder.set("notify_result", Constant.ERROR).set("notify_msg", "无响应！");
		}else {
			payOrder.set("notify_result",retList.get(0).get(Constant.RESULT)).set("notify_msg", retList.get(0).get(Constant.MESSAGE));
		}
		payOrder.update();
	}
	
	/*************
	 *Order分页
	**************/
	public Page<PayOrder> getOrderPage(Client client,Map<String,String[]> paramMap){
		String[][] param = new String[][]{
			new String[]{"num",like,"num"}				
			,new String[]{"model",like,"model"}				
			,new String[]{"title",like,"title"}				
			,new String[]{"pay_channel",like,"pay_channel"}				
			,new String[]{"pay_num",like,"pay_num"}
			,new String[]{"start_time",gte,"create_time"}
			,new String[]{"end_time",lte,"create_time"}
		};
		Page<PayOrder> pd = PayOrder.dao.paginate(
				getParaToInt(paramMap, client.getPageNumber(), 1), 
				getParaToInt(paramMap,client.getPageSize(),20),
				"select *","from "+PayOrder.tablename+" where 1=1 " 				
				+ whereSql(paramMap,param)
				+ orderby(client, paramMap),whereParam(paramMap, param));
		changeBasedata(pd.getList(), new String[]{"status","PAY_STATUS"}
			,new String[]{"pay_channel","PAY_CHANNEL"});
		return pd;
	}

}
